using System;
using System.Windows.Forms;
using System.Xml;

namespace FB.WinForms.Controls
{
	/// <summary>
	/// Represents an extended ComboBox control that can display simplified HTML in the listbox portion and
	/// HTML in the textbox portion.
	/// </summary>
	public sealed class HtmlDropDownListSimpleStyle : HtmlDropDownList
	{
		private int mSelectionLength;
		private int mSelectionStart;

		/// <summary>
		/// Initialise a new instance of the <see cref="HtmlDropDownListSimpleStyle"/> class.
		/// </summary>
		public HtmlDropDownListSimpleStyle()
		{
			SetDropDownStyle(ComboBoxStyle.Simple);
		}

		/// <summary>
		/// Gets or sets the number of characters selected in the text box.
		/// </summary>
		/// <remarks>
		/// This value is correct even if the control does not have focus.
		/// </remarks>
		public new int SelectionLength
		{
			get { return mSelectionLength; }
			set { mSelectionLength = value; }
		}
	
		/// <summary>
		/// Gets or sets the starting point of text selected in the text box.
		/// </summary>
		/// <remarks>
		/// This value is correct even if the control does not have focus.
		/// </remarks>
		public new int SelectionStart
		{
			get { return mSelectionStart; }
			set { mSelectionStart = value; }
		}

		/// <summary>
		/// Gets or sets a value indicating the currently selected text in the control.
		/// </summary>
		/// <remarks>
		/// This value is correct even if the control does not have focus.
		/// </remarks>
		public new string SelectedText
		{
			get 
			{ 
				base.SelectionStart = this.SelectionStart;
				base.SelectionLength = this.SelectionLength;
				return base.SelectedText; 
			}
			set 
			{ 
				base.SelectionStart = this.SelectionStart;
				base.SelectionLength = this.SelectionLength;
				base.SelectedText = value;
			}
		}

		/// <summary>
		/// Handles the base class <c>Control.OnGotFocus</c> event so override the standard
		/// combobox behaviour of selecting all the tex in the combobox.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnGotFocus(EventArgs e)
		{
			base.SelectionStart = this.SelectionStart;
			base.SelectionLength = this.SelectionLength;
			base.OnGotFocus (e);
		}
	
		/// <summary>
		/// Handles the base class <c>Control.OnKeyUp</c> event so remember the SelectionStart
		/// and SelectionLength values.
		/// </summary>
		/// <param name="e">A KeyEventArgs that contains the event data.</param>
		protected override void OnKeyUp(KeyEventArgs e)
		{
			this.SelectionStart = base.SelectionStart;
			this.SelectionLength = base.SelectionLength;
			base.OnKeyUp (e);
		}
	
		/// <summary>
		/// Handles the base class <c>Control.OnMouseUp</c> event so remember the SelectionStart
		/// and SelectionLength values.
		/// </summary>
		/// <param name="e">A MouseEventArgs that contains the event data.</param>
		protected override void OnMouseUp(MouseEventArgs e)
		{
			this.SelectionStart = base.SelectionStart;
			this.SelectionLength = base.SelectionLength;
			base.OnMouseUp (e);
		}
	}
}